'''
Created on Apr 29, 2010

This module contains the sprite classes for IDEAvolution.

@author: dkadish
'''
from pygame import draw, sprite, surface
from settings import SPRITE_BACKGROUND_COLOUR, SPRITE_SIZE, SPRITE_ALPHA_COLOUR, \
                     LINK_LINE_COLOUR, LINE_ALPHA_COLOUR, \
                     BACKGROUND_COLOUR
import math

class IdeaSprite(sprite.Sprite):
    '''
    '''
    
    def __init__(self, idea, position):
        sprite.Sprite.__init__(self)
        
        self.idea = idea
        
        self.color = SPRITE_BACKGROUND_COLOUR
        self.image = surface.Surface(SPRITE_SIZE)
        self.image.set_colorkey(SPRITE_ALPHA_COLOUR)
        self.image.fill(self.color)
        self.rect = self.image.get_rect()
        self.rect.center = position
        
    @property
    def position(self):
        return self.rect.center
    
    @position.setter
    def position(self, pos):
        self.rect.center = pos
        
    def update(self):
        self.position = self.idea.position
        
class LinkSprite(sprite.Sprite):
    '''
    '''
    
    def __init__(self, link):
        sprite.Sprite.__init__(self)
        
        self.link = link
        
        idea1 = self.link.ideas[0].sprite
        idea2 = self.link.ideas[1].sprite
        
        x = idea1.position[0] - idea2.position[0]
        y = idea1.position[1] - idea2.position[1]
        
        self.color = LINK_LINE_COLOUR
        self.image = surface.Surface((math.fabs(x), math.fabs(y)))
        self.image.set_colorkey(LINE_ALPHA_COLOUR)
        self.image.fill(BACKGROUND_COLOUR)
        
        self.update_position()
        
        if x*y > 0:
            draw.aaline(self.image,self.color, (0, 0),(self.rect.right-self.rect.left,self.rect.bottom-self.rect.top))
        else:
            draw.aaline(self.image,self.color, (0, self.rect.bottom-self.rect.top),(self.rect.right-self.rect.left,0))
        
    def update(self):
        self.update_position()
        
    def update_position(self):
        idea1 = self.link.ideas[0].sprite
        idea2 = self.link.ideas[1].sprite
        
        x = idea1.position[0] - idea2.position[0]
        y = idea1.position[1] - idea2.position[1]
        
        self.rect = self.image.get_rect()
        self.rect.left = min([idea1.position[0], idea2.position[0]])
        self.rect.top = min([idea1.position[1], idea2.position[1]])